<?php
use think\migration\Migrator;

class ExamMenu extends Migrator
{
    public function change(): void
    {
        // 使用应用内的 Menu 帮助类来创建后台菜单项
        $menus = [
            [
                'type' => 'menu_dir',
                'title' => '考试管理',
                'name' => 'exam',
                'path' => 'exam',
                'children' => [
                    [
                        'type' => 'menu',
                        'title' => '题库管理',
                        'name' => 'exam/question',
                        'path' => 'exam/question',
                        'menu_type' => 'tab',
                        'keepalive' => 1,
                        'component' => '/src/views/backend/exam/question.vue',
                    ],
                    [
                        'type' => 'menu',
                        'title' => '试卷管理',
                        'name' => 'exam/paper',
                        'path' => 'exam/paper',
                        'menu_type' => 'tab',
                        'keepalive' => 1,
                        'component' => '/src/views/backend/exam/paper.vue',
                    ],
                    [
                        'type' => 'menu',
                        'title' => '简答批阅',
                        'name' => 'exam/review',
                        'path' => 'exam/review',
                        'menu_type' => 'tab',
                        'keepalive' => 0,
                        'component' => '/src/views/backend/exam/review.vue',
                    ],
                ],
            ],
        ];

        // 直接调用应用 library，若在迁移环境无法加载应用类则捕获异常并写日志
        try {
            \app\common\library\Menu::create($menus, 0, 'ignore');
        } catch (\Throwable $e) {
            // 在迁移时输出错误到控制台，但不抛出异常
            echo "Failed to create menu: " . $e->getMessage() . "\n";
        }
    }
}
